package com.lancoo.znbkxx.teacher.ui.fragment

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.ReviewedDetail
import com.lancoo.znbkxx.teacher.model.data.StuProgressInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.view_stuprogress.*

/**
 * Created by wealive on 2021/3/9.
 *
 *
 */
class StuProgressDialog : AppCompatDialogFragment() {
    private val type by lazy { requireArguments().getInt("type") }
    private val stuInfo by lazy {
        requireArguments().getParcelableArrayList<StuProgressInfo>("info")
    }
    private val detailInfo by lazy { requireArguments().getParcelable<ReviewedDetail>("reviewed") }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return inflater.inflate(R.layout.view_stuprogress, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        vp.offscreenPageLimit = 1
        vp.adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return 2
            }

            override fun createFragment(position: Int): Fragment {
                return StuProgressFragment().apply {
                    when (position) {
                        0 -> arguments = when (type) {
                            0 -> bundleOf(Pair("info",
                                    stuInfo?.filter { it.State == 2 || it.State == 3 }))
                            1 -> bundleOf(Pair("info",
                                    stuInfo?.filter { it.State == 3 }))
                            else -> bundleOf(Pair("info",
                                    detailInfo?.ReviewedList))
                        }
                        else -> arguments = when (type) {
                            0 -> bundleOf(Pair("info",
                                    stuInfo?.filter { it.State == 0 || it.State == 1 }))
                            1 -> bundleOf(Pair("info",
                                    stuInfo?.filter { it.State != 3 }))
                            else -> bundleOf(Pair("info",
                                    detailInfo?.NotReviewedList))
                        }
                    }
                }
            }
        }
        TabLayoutMediator(tabLayout, vp) { tab, position ->
            tab.text = when (position) {
                0 -> when (type) {
                    0 -> "已提交学生"
                    1 -> "已评阅学生"
                    else -> "已巩固学生"
                }
                else -> when (type) {
                    0 -> "未提交学生"
                    1 -> "未评阅学生"
                    else -> "未巩固学生"
                }
            }
        }.attach()
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = DensityUtils.dp2px(context, 300f)
        param?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialog?.window?.attributes = param
    }
}